package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @BelongsProject: sky-take-out
 * @BelongsPackage: com.sky.controller.admin
 * @Author: zhouzhijie
 * @CreateTime: 2023-11-13  16:31
 * @Description: 菜品管理
 * @Version: 1.0
 */
@RestController
@RequestMapping("admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @description: 新增菜品
     * @author: zhouzhijie
     * @date: 2023/11/13 16:34
     * @param: [dishDTO]
     * @return:
     **/
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);

        //清理缓存数据
        String key = "dish_"+dishDTO.getCategoryId();
//        redisTemplate.delete(key);
        cleanCache(key);
        return Result.success();
    }

    /**
     * @description: 菜品分页查询
     * @author: zhouzhijie
     * @date: 2023/11/15 9:06
     * @param: [dishPageQueryDTO]
     * @return:
     **/
    @GetMapping("page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询:{}",dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @description: 菜品批量删除
     * @author: zhouzhijie
     * @date: 2023/11/15 14:43
     * @param: [ids]
     * @return:
     **/
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result deleteBatch(@RequestParam List<Long> ids){
        log.info("菜品批量删除:{}",ids);
        dishService.deleteBatch(ids);
        //将所有菜品缓存数据清理掉，所有以dish_开头的key(之所以全部删除是因为没必要再查询获取对应的分类categoryId)
        //将所有的keys获取出来，因为删除不支持通配符*
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * @description: 根据id查询菜品和对应的口味
     * @author: zhouzhijie
     * @date: 2023/11/15 17:13
     * @param: [id]
     * @return:
     **/
    @GetMapping("{id}")
    @ApiOperation("根据id查询菜品和对应的口味")
    public Result<DishVO> getByIdWithFlavor(@PathVariable Long id){
        log.info("根据id查询菜品和对应的口味:{}",id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * @description: 修改菜品
     * @author: zhouzhijie
     * @date: 2023/11/16 17:58
     * @param: [dishDTO]
     * @return:
     **/
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);

        //将所有菜品缓存数据清理掉，所有以dish_开头的key
        // (之所以全部删除是因为可能修改分类，涉及两份缓存数据，修改也不是经常操作，没必要再查询数据库获取对应的分类)
        //将所有的keys获取出来，因为删除不支持通配符*
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * @description: 根据分类id查找菜品
     * @author: zhouzhijie
     * @date: 2023/11/17 8:57
     * @param: [categoryId]
     * @return:
     **/
    @GetMapping("list")
    @ApiOperation("根据分类id查找菜品")
    public Result<List<Dish>> list(Long categoryId){
        log.info("根据分类id查找菜品:{}",categoryId);
        List<Dish> dishes = dishService.list(categoryId);
        return Result.success(dishes);
    }

    /**
     * @description: 菜品起售、停售
     * @author: zhouzhijie
     * @date: 2023/11/17 10:18
     * @param: [status, id]
     * @return:
     **/
    @PostMapping("status/{status}")
    @ApiOperation("菜品起售、停售")
    public Result startOrStop(@PathVariable("status") Integer status,Long id){
        log.info("菜品起售、停售:{}",id);
        dishService.startOrStop(status,id);

        //将所有菜品缓存数据清理掉，所有以dish_开头的key
        // (可以删除对应的缓存，但需要额外的根据菜品id查询菜品获取对应的分类categoryId，得不偿失，没必要)
        //将所有的keys获取出来，因为删除不支持通配符*
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * @description: 清理缓存数据
     * @author: zhouzhijie
     * @date: 2023/11/21 17:30
     * @param: [pattern]
     * @return:
     **/
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
